
package code.codeworld.user.user.controller;


import code.codeworld.user.user.model.User;
import code.codeworld.user.user.model.vo.UserVo;
import code.codeworld.user.user.service.UserService;
import code.codeworld.common.kits.Request;
import code.codeworld.common.kits.Result;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

/**
 * world控制器
 *
 * @author freecode
 * @date 2023/03/23 00:20
 */
@CrossOrigin
@RequestMapping(Request.CODEWORLD_WEB)
@RestController
@Validated
public class UserCtl {
    @Resource
    private UserService UserService;

    /**
     * 登录User
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @PostMapping("user/login")
    public Result<String> login(@RequestBody @Valid User user) {
        if (StringUtils.hasText(user.getName
                ()) || StringUtils.hasText(user.getPassword())) {
            return Result.paramError();
        }
        UserService.edit(user);
        return Result.ok();
    }

    /**
     * 登出User
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @PostMapping("user/logout")
    public Result<String> logout(@RequestBody @Valid User user) {
        if (user.getId() == null) {
            return Result.paramError();
        }
        UserService.edit(user);
        return Result.ok();
    }

    /**
     * 添加User
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @PostMapping("user/add")
    public Result<String> add(@RequestBody @Valid User User) {
        UserService.add(User);
        return Result.ok();
    }

    /**
     * 获取User分页
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @GetMapping("user/page")
    public Page<User> page(UserVo userVo) {
        Integer pageSize = userVo.getPageSize();
        Integer pageNum = userVo.getPageNum();
        if (pageSize == null) {
            pageSize = BigInteger.TEN.intValue();
        }
        if (pageNum == null || pageNum <= BigInteger.ONE.intValue()) {
            pageNum = BigInteger.ZERO.intValue();
        } else {
            pageNum = pageNum - BigInteger.ONE.intValue();
        }
        userVo.setPageable(PageRequest.of(pageNum, pageSize));
        return UserService.page(userVo);
    }

    /**
     * 获取User详情
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @GetMapping("user/detail")
    public Result<User> detail(@Valid User user) {
        return Result.data(UserService.detail(user));
    }

    /**
     * 编辑User
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @PostMapping("user/edit")
    public Result<String> edit(@RequestBody @Valid User user) {
        if (user.getId() == null) {
            return Result.paramError();
        }
        UserService.edit(user);
        return Result.ok();
    }

    /**
     * 删除User
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @PostMapping("user/delete")
    public Result<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                         List<User> UserList) {
        UserService.delete(UserList);
        return Result.ok();
    }

}
